package ws

import (
	"blockchain_data/config"
	"blockchain_data/handler/model"
	"blockchain_data/handler/okex/response"
	okmodel "blockchain_data/handler/okex/ws/model"
	"encoding/json"
	"reflect"
	"time"
)

type WSMarketClient struct {
	OkWebSocketOp
}

func (wsMk *WSMarketClient) Init() *WSMarketClient {
	var path string
	path = config.OkexWsPublicHost
	wsMk.Open(path, "", true, 30 * time.Second)
	return wsMk
}

type OnSubTradeDetailResponse func(trade *response.Trade)

//订阅tick数据
func (wsMk *WSMarketClient) SubTradeDetail(contractCode string,ch chan model.Msg,callbackFun OnSubTradeDetailResponse) {
	args := okmodel.Arg{
		Channel: "trades",
		InstId: contractCode,
	}
	subData := okmodel.WSSubData{
		Op: "subscribe",
		Args: []okmodel.Arg{args},
	}
	wsMk.Ch = ch
	jdata, _ := json.Marshal(subData)
	wsMk.Sub(jdata, callbackFun, reflect.TypeOf(response.Trade{}))
}
